package com.fenghongzhang.zhoukao2;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


/**
 * A simple {@link Fragment} subclass.
 */
public class OneFragment extends Fragment {
    private Button write;
    private Button read;

    public OneFragment() {
        // Required empty public constructor
    }

    private static final String TAG = "OneFragment";

    public interface MyListener{
        void click(int i,String s);
    }

    private MyListener myListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        myListener = (MyListener) getActivity();
        View inflate = inflater.inflate(R.layout.fragment_one, container, false);

        write = (Button) inflate.findViewById(R.id.btn);
        read = (Button) inflate.findViewById(R.id.btn2);



        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //判断是否挂载
                String state = Environment.getExternalStorageState();
                if(state.equals(Environment.MEDIA_MOUNTED)){
                    File path = Environment.getExternalStorageDirectory();
                    Log.i(TAG, "onClick: "+path);
                    FileOutputStream fileOutputStream = null;
                    try {
                         fileOutputStream = new FileOutputStream(new File(path, "daxia.txt"));
                        fileOutputStream.write("dsafa".getBytes());
                        fileOutputStream.flush();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        try {
                            if(fileOutputStream != null){
                                fileOutputStream.close();
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });

        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                myListener.click(1,"爱的方式懂法守法阿斯顿发");
            }
        });


        return inflate;
    }
}
